<?php
/**
 * User signup form.
 *
 * @category Zend
 * @package Form
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 * @license GPL v3
 */

class Application_Form_Login extends Twitter_Form
{
    const USERNAME_INVALID = 'Username is not valid.';
    const PASSWORD_INVALID = 'Password is not valid.';
    
    protected $_usernameAlnum = array(
        Zend_Validate_Alnum::INVALID => self::USERNAME_INVALID,
        Zend_Validate_Alnum::NOT_ALNUM => self::USERNAME_INVALID
    );
    
    protected $_usernameLength = array(
        Zend_Validate_StringLength::TOO_SHORT => self::USERNAME_INVALID,
        Zend_Validate_StringLength::TOO_LONG => self::USERNAME_INVALID
    );
    
    protected $_passwordLength = array(
        Zend_Validate_StringLength::TOO_SHORT => self::PASSWORD_INVALID,
        Zend_Validate_StringLength::TOO_LONG => self::PASSWORD_INVALID
    );
    
    /**
     * Setup form elements
     */
    public function init()
    {
        $this->setLegend('LOGIN');
        $this->setAttrib('horizontal', true);
        $this->setAttrib('id', 'login-form');
        
        $this->addElement('text', 'username', array(
            'label' => 'Username',
            'description' => 'Enter your Disputeo username.',
            'required' => true,
            'validators' => array(
                array(
                    'validator' => 'Alnum',
                    'options' => array(
                        'messages' => $this->_usernameAlnum
                    )
                ),
                array(
                    'validator' => 'StringLength',
                    'options' => array(
                        'max' => 32,
                        'min' => 4,
                        'encoding' => 'UTF-8',
                        'messages' => $this->_usernameLength
                    )
                )
            )
        ));
        
        $this->addElement('password', 'password', array(
            'label' => 'Password',
            'description' => 'Enter your Disputeo password.',
            'required' => true,
            'validators' => array(
                array(
                    'validator' => 'StringLength',
                    'options' => array(
                        'max' => 32,
                        'min' => 7,
                        'encoding' => 'UTF-8',
                        'messages' => $this->_passwordLength
                    )
                )
            )
        ));
        
        $this->addElement('checkbox', 'rememberme', array(
            'label' => 'Remember Me',
            'checked' => true
        ));
        
        $this->addElement('submit', 'login', array(
            'label' => 'Login',
            'class' => 'btn'
        ));
        
        
        $this->addElement('hidden', 'referer');
    }

    /**
     * Set password error
     */
    public function authFailed()
    {
        $this->getElement('password')
            ->setErrors(array(self::PASSWORD_INVALID));
    }
}

